// pages/module/order/commitOrder/commitOrder.js
import regeneratorRuntime from '../../../../utils/runtime.js';
import { getData, setData, clearData, dataKey } from '../../../../utils/storage.js';
import {
  fetchOpenId,
} from '../../../../utils/imapi.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    networkType: 1,
    show: 0,
    showText: 0,
    focus: '',
    remark: '',
    model: 0, //是否展示密码输入层
    useFyb: false,
    pwdVal: '', //获取输入的密码
    payFocus: true, //文本框焦点
    showMoney: 0,
    psd_id: '1',
    pwdError: 0,
    password: '', //存放第一次输入的密码
    is_setAddress: 0,
    buy_user_address: '',
    buy_user_mobile: '',
    buy_user_name: '',
    address_id: '',
    data: {},
    showMask: 0,
    qustion: 0,
    agreementMask: 0,
    agreement: '',
    switchValue: 0,
    currentDay: 0,
    id: 0,
    clickAgreement: 0,
    day: 0,
    clickPay: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var url = 'api/shopCart/showOrderConfirm';
    var data = {};
    // data.id = 10023;
    login.login(that, options, () => {
      req.get(that, url, data, function (res) {
        var data = res.data.data;
        var showMoney = 0;
        if (data.credit_money >= data.need_pay_total) {
          showMoney = 1;
        } else {
          showMoney = 0;
        }
        that.setData({
          address_id: data.address_id,
          buy_user_address: data.address,
          buy_user_mobile: data.mobile,
          buy_user_name: data.user_name,
          showMoney,
          show_pay_total: data.need_pay_total,
          data: data,
          show: 1,
        })
        if (that.data.address_id) {
          that.setData({
            is_setAddress: 1
          })
        }
      })
    })

  },

  contentClick() {
    this.setData({
      focus: 1,
      showText: 1
    })
  },

  //  显示支付密码输入层

  show() {

    let that = this;

    that.setData({

      model: true,

      payFocus: true,

    });

  },

  getRemark(e) {
    // this.data.remark = e.detail.value;
    this.setData({
      remark: e.detail.value,
      focus: 0,
      showText: 0
    })
  },

  useFyb() {
    var that = this;
    if (!that.data.data.has_credit) {
      wx.navigateTo({
        url: '/pages/module/mySet/myFyb/agreement/agreement',
      })
    } else {
      this.setData({
        useFyb: !this.data.useFyb
      })
      if (this.data.useFyb) {
        if (this.data.showMoney) {
          this.setData({
            show_pay_total: 0
          })
        } else {
          this.setData({
            show_pay_total: this.data.data.need_pay_total - this.data.data.credit_money
          })
        }
      } else {
        this.setData({
          show_pay_total: this.data.data.need_pay_total
        })
      }
    }
  },

  //  隐藏支付密码输入层  
  hide() {
    let that = this;
    this.setData({
      model: false,
      payFocus: false,
      pwdVal: '',
      pwdError: 0,
      clickPay: 1,
    });
  },


  // 获取焦点

  getFocus: function () {

    this.setData({
      payFocus: true
    });

  },


  aboutFyb() {
    wx.navigateTo({
      url: '/pages/module/mySet/question/question?fyb=1'
    })
  },

  // 输入密码监听

  inputPwd: function (e) {
    var that = this;
    this.setData({
      pwdVal: e.detail.value,
      pwdError: 0
    });
    if (this.data.pwdVal.length >= 6) {
      that.wxPay(that);
    }
  },





  // 添加地址
  addAdress: function () {
    wx.getSetting({
      success(res) {
        if ('scope.address' in res.authSetting && !res.authSetting['scope.address']) {
          wx.openSetting({
            success: (res) => {

            },
            fail: (res) => {

            }
          })
          return;
        }
      }
    })
    var that = this;
    var url = "api/address/addAddress";
    var data = {};
    wx.chooseAddress({
      success: function (res) {
        data.userName = res.userName;
        data.postalCode = res.postalCode;
        data.provinceName = res.provinceName;
        data.cityName = res.cityName;
        data.countyName = res.countyName;
        data.detailInfo = res.detailInfo;
        data.nationalCode = res.nationalCode;
        data.telNumber = res.telNumber;
        that.setData({
          buy_user_address: res.provinceName + res.cityName + res.countyName + res.detailInfo,
          buy_user_name: res.userName,
          buy_user_mobile: res.telNumber
        })
        req.get(that, url, data,
          function (res) {
            if (res.data.returnCode == 0) {
              that.setData({
                address_id: res.data.data,
                is_setAddress: 1
              })
            } else {
              wx.showToast({
                title: res.data.message
              })
            }
          },
          function () {});
      },
      fail: function (err) {

      }
    })
  },

  showMask(e) {
    var id = e.currentTarget.dataset.id;
    this.setData({
      showMask: 1,
      qustion: id
    })
  },
  // 用户协议
  agreement() {
    var that = this;
    if (that.data.agreement) {
      that.setData({
        agreementMask: 1,
      })
    } else {
      var url = 'api/system/leaseagreement';
      var data = {};
      req.get(that, url, data, function (res) {
        that.setData({
          agreement: res.data.data,
          agreementMask: 1,
        })
      })
    }
  },
  // 关闭弹窗
  closeMask() {
    this.setData({
      showMask: 0,
      agreementMask: 0,
    })
  },
  // 选择按钮
  switchChange(e) {
    // console.log(e);
    var val = e.detail.value ? '1' : '0'
    this.setData({
      switchValue: val
    })
  },

  // 勾选协议
  clickAgreement() {
    this.setData({
      clickAgreement: !this.data.clickAgreement
    })
  },
  // 订单支付
  orderPay() {
    var that = this;
    // console.log(that.data.clickPay)
    if (that.data.clickPay == 1) {
      that.data.clickPay = 0;
      if (that.data.useFyb == 1) {

        this.setData({
          model: true,
          payFocus: true,
        })
      } else {
        that.wxPay(that);
      }
    }
  },

  editPwd() {
    this.setData({
      model: false,
      payFocus: false,
      clickPay: 1,
      pwdVal: '',
    });
    wx.navigateTo({
      url: '/pages/module/mySet/myFyb/setPayPwd/setPayPwd',
    })
  },

  wxPay(that) {
    var url = 'api/shopCart/commitPayShopCart';
    var data = {
      remark: that.data.remark,
      pay_password: that.data.pwdVal,
      top_order_id: that.data.data.top_order_id,
      address_id: that.data.address_id,
      member_coupon_id: that.data.data.member_coupon_id,
      ship_member_coupon_id: that.data.data.ship_member_coupon_id,
      is_insurance: that.data.switchValue,
      use_credit: that.data.useFyb ? '1' : '0'
    };
    req.get(that, url, data, res => {
      let data = res.data.data;
      that.setData({
        model: 0
      })
      that.data.clickPay = 1;
      if (data.pay_status) {
        wx.redirectTo({
          url: '../paySuccess/paySuccess?top_order_no=' + data.top_order_no,
        })
      } else {
        that.data.order_id = data.order_id;
        if (res.data.returnCode == 0) {
          wx.requestPayment({ //调起微信支付
            'timeStamp': data.timeStamp,
            'nonceStr': data.nonceStr,
            'package': data.package,
            'signType': 'MD5',
            'paySign': data.paySign,
            'success': function (res) {
              that.data.clickPay = 1;
              wx.redirectTo({
                url: '../paySuccess/paySuccess?top_order_no=' + data.top_order_no,
              })
            },
            'fail': function (res) {
              that.data.clickPay = 1;
              wx.showToast({
                title: '支付取消',
              })
            }
          })
        }
      }
    }, res => {
      that.data.clickPay = 1;
      console.log(res)
      if (res.data.returnCode == '-2') {
        that.setData({
          pwdError: 1,
          payFocus: true,
          pwdVal: ''
        })
      } else {
        this.setData({
          model: false
        })
      }
    })
  },

  // 存储formid
  sendFormId(e) {
    var that = this;
    var form_id = e.detail.formId;
    var url = 'api/log/emptynull';
    var data = {
      formid: form_id
    };
    req.get(that, url, data, (res) => {})
  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (getApp().globalData.refresh) {
      var that = this;
      var url = 'api/shopCart/showOrderConfirm';
      var data = {};
      req.get(that, url, data, function (res) {
        var data = res.data.data;
        var showMoney = 0;
        if (data.credit_money >= data.need_pay_total) {
          showMoney = 1;
        } else {
          showMoney = 0;
        }
        that.setData({
          address_id: data.address_id,
          buy_user_address: data.address,
          buy_user_mobile: data.mobile,
          buy_user_name: data.user_name,
          showMoney,
          show_pay_total: data.need_pay_total,
          data: data,
        })
        if (that.data.address_id) {
          that.setData({
            is_setAddress: 1
          })
        }
      })

    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})